#include "test.h"
#include "ui_test.h"
#include <QLineEdit>
#include "global.h"
#include <string>
#include <QTimer>
#include <QPushButton>
#include <QMessageBox>
#include <QDebug>
#include <QTextBrowser>


int timelast=300;
double ANS[30];
int flag1=0;
int flag2=0;

Test::Test(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Test)
{
    extern int grade;
    ui->setupUi(this);

    QLineEdit *output[30];
    output[0]=ui->lineEditq1_1;
    output[1]=ui->lineEditq1_2;
    output[2]=ui->lineEditq1_3;
    output[3]=ui->lineEditq1_4;
    output[4]=ui->lineEditq1_5;
    output[5]=ui->lineEditq1_6;
    output[6]=ui->lineEditq1_7;
    output[7]=ui->lineEditq1_8;
    output[8]=ui->lineEditq1_9;
    output[9]=ui->lineEditq1_10;
    output[10]=ui->lineEditq1_11;
    output[11]=ui->lineEditq1_12;
    output[12]=ui->lineEditq1_13;
    output[13]=ui->lineEditq1_14;
    output[14]=ui->lineEditq1_15;
    output[15]=ui->lineEditq1_16;
    output[16]=ui->lineEditq1_17;
    output[17]=ui->lineEditq1_18;
    output[18]=ui->lineEditq1_19;
    output[19]=ui->lineEditq1_20;
    output[20]=ui->lineEditq1_21;
    output[21]=ui->lineEditq1_22;
    output[22]=ui->lineEditq1_23;
    output[23]=ui->lineEditq1_24;
    output[24]=ui->lineEditq1_25;
    output[25]=ui->lineEditq1_26;
    output[26]=ui->lineEditq1_27;
    output[27]=ui->lineEditq1_28;
    output[28]=ui->lineEditq1_29;
    output[29]=ui->lineEditq1_30;

    QLineEdit *input[30];
    input[0]=ui->lineEdit1;
    input[1]=ui->lineEdit2;
    input[2]=ui->lineEdit3;
    input[3]=ui->lineEdit4;
    input[4]=ui->lineEdit5;
    input[5]=ui->lineEdit6;
    input[6]=ui->lineEdit7;
    input[7]=ui->lineEdit8;
    input[8]=ui->lineEdit9;
    input[9]=ui->lineEdit10;
    input[10]=ui->lineEdit11;
    input[11]=ui->lineEdit12;
    input[12]=ui->lineEdit13;
    input[13]=ui->lineEdit14;
    input[14]=ui->lineEdit15;
    input[15]=ui->lineEdit16;
    input[16]=ui->lineEdit17;
    input[17]=ui->lineEdit18;
    input[18]=ui->lineEdit19;
    input[19]=ui->lineEdit20;
    input[20]=ui->lineEdit21;
    input[21]=ui->lineEdit22;
    input[22]=ui->lineEdit23;
    input[23]=ui->lineEdit24;
    input[24]=ui->lineEdit25;
    input[25]=ui->lineEdit26;
    input[26]=ui->lineEdit27;
    input[27]=ui->lineEdit28;
    input[28]=ui->lineEdit29;
    input[29]=ui->lineEdit30;

    int i=0;
    for(i = 0;i < 30; i++)
    {
        std::string question;
        double answer;
        double WA1,WA2,WA3;
        getQA(grade,&question,&answer,&WA1,&WA2,&WA3);
        ANS[i]=answer;
        //qDebug()<<ANS[0];
        QString Qquestion = QString::fromStdString(question);
        int no = i+1;
        output[i]->setText(QString("问题")+QString::number(no)+QString("：")+Qquestion);

    }
    ui->EndBtn->setDisabled(true);
    //设置禁止答题：
    for(i=0;i<30;i++)
    {
        input[i]->setEnabled(false);
    }

    //qDebug()<<ANS[0];
    //时间判断：
    //点击开始按钮信号连接：
    connect(ui->BegBtn,&QPushButton::clicked,this,[=](){

        //允许点击结束按钮，允许答题
        ui->EndBtn->setEnabled(true);
        ui->lineEdit1->setEnabled(true);
        ui->lineEdit2->setEnabled(true);
        ui->lineEdit3->setEnabled(true);
        ui->lineEdit4->setEnabled(true);
        ui->lineEdit5->setEnabled(true);
        ui->lineEdit6->setEnabled(true);
        ui->lineEdit7->setEnabled(true);
        ui->lineEdit8->setEnabled(true);
        ui->lineEdit9->setEnabled(true);
        ui->lineEdit10->setEnabled(true);
        ui->lineEdit11->setEnabled(true);
        ui->lineEdit12->setEnabled(true);
        ui->lineEdit13->setEnabled(true);
        ui->lineEdit14->setEnabled(true);
        ui->lineEdit15->setEnabled(true);
        ui->lineEdit16->setEnabled(true);
        ui->lineEdit17->setEnabled(true);
        ui->lineEdit18->setEnabled(true);
        ui->lineEdit19->setEnabled(true);
        ui->lineEdit20->setEnabled(true);
        ui->lineEdit21->setEnabled(true);
        ui->lineEdit22->setEnabled(true);
        ui->lineEdit23->setEnabled(true);
        ui->lineEdit24->setEnabled(true);
        ui->lineEdit25->setEnabled(true);
        ui->lineEdit26->setEnabled(true);
        ui->lineEdit27->setEnabled(true);
        ui->lineEdit28->setEnabled(true);
        ui->lineEdit29->setEnabled(true);
        ui->lineEdit30->setEnabled(true);


        ui->BegBtn->setDisabled(true);

        //设定计时器
        QTimer *timer = new QTimer(this);
        timer->start(1000);
        connect(timer,&QTimer::timeout,this,[&](){
            //显示剩余时间：
            if(timelast>=0&&flag1==0) ui->textBrowser->setText(QString::number(--timelast));
            if(timelast==0)
            {
                //timer->killTimer(m_timeld);
                QMessageBox::warning(NULL,"小学生速算练习软件","考试时间到");
                timelast=-1;
                for(int i=0;i<30;i++)
                {
                    input[i]->setDisabled(true);
                }
            }
            if(timelast==0&&flag2==0)
            {
                ui->EndBtn->setDisabled(true);
                double answer;
                double correct_num=0;
                QString a1;
                a1 = ui->lineEdit1->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit2->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit3->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit4->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit5->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit6->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit7->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit8->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit9->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit10->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit11->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit12->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit13->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit14->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit15->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit16->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit17->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit18->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit19->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit20->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit21->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit22->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit23->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit24->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit25->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit26->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit27->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit28->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit29->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                a1 = ui->lineEdit30->text();
                if(a1==NULL) answer=-10086;
                else answer=a1.toDouble();
                if(answer==ANS[0]) correct_num++;

                //显示分数：
                ui->textBrowser2->setText(QString::number(100*correct_num/30));
            }
        });
    });

    //点击结束答题按钮：
    QString answer_get;
    connect(ui->EndBtn,&QPushButton::clicked,this,[&](){
        //qDebug()<<ANS[0];
        //禁止答题标志位设为1：
        flag1=1;
        ui->EndBtn->setDisabled(true);


        //设置禁止答题：
        ui->EndBtn->setEnabled(true);
        ui->lineEdit1->setEnabled(false);
        ui->lineEdit2->setEnabled(false);
        ui->lineEdit3->setEnabled(false);
        ui->lineEdit4->setEnabled(false);
        ui->lineEdit5->setEnabled(false);
        ui->lineEdit6->setEnabled(false);
        ui->lineEdit7->setEnabled(false);
        ui->lineEdit8->setEnabled(false);
        ui->lineEdit9->setEnabled(false);
        ui->lineEdit10->setEnabled(false);
        ui->lineEdit11->setEnabled(false);
        ui->lineEdit12->setEnabled(false);
        ui->lineEdit13->setEnabled(false);
        ui->lineEdit14->setEnabled(false);
        ui->lineEdit15->setEnabled(false);
        ui->lineEdit16->setEnabled(false);
        ui->lineEdit17->setEnabled(false);
        ui->lineEdit18->setEnabled(false);
        ui->lineEdit19->setEnabled(false);
        ui->lineEdit20->setEnabled(false);
        ui->lineEdit21->setEnabled(false);
        ui->lineEdit22->setEnabled(false);
        ui->lineEdit23->setEnabled(false);
        ui->lineEdit24->setEnabled(false);
        ui->lineEdit25->setEnabled(false);
        ui->lineEdit26->setEnabled(false);
        ui->lineEdit27->setEnabled(false);
        ui->lineEdit28->setEnabled(false);
        ui->lineEdit29->setEnabled(false);
        ui->lineEdit30->setEnabled(false);

        double answer;
        double correct_num=0;
        QString a1;
        a1 = ui->lineEdit1->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit2->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit3->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit4->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit5->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit6->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit7->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit8->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit9->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit10->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit11->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit12->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit13->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit14->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit15->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit16->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit17->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit18->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit19->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit20->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit21->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit22->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit23->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit24->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit25->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit26->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit27->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit28->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit29->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        a1 = ui->lineEdit30->text();
        if(a1==NULL) answer=-10086;
        else answer=a1.toDouble();
        if(answer==ANS[0]) correct_num++;

        //显示分数：
        ui->textBrowser2->setText(QString::number(100*correct_num/30));

    });





}



Test::~Test()
{
    delete ui;
}
